package com.hua.yuan.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hua.yuan.R;
import com.hua.yuan.ui.ImageDetailActivity;
import com.hua.yuan.ui.data.PictureDataRequest;
import com.hua.yuan.ui.module.ImageDetailCommonBean;
import com.hua.yuan.util.GlideCircleTransform;
import com.hua.yuan.util.ToastUtil;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import okhttp3.Call;

/**
 * Created by huangyang on 2018/5/9.
 * 评论
 */

public class ImageCommonAdapter extends BaseAdapter {

    private Context context;

    public ImageCommonAdapter(Context context) {
        this.context = context;
    }

    private List<ImageDetailCommonBean> list;

    public List<ImageDetailCommonBean> getList() {
        return list;
    }

    public void setList(List<ImageDetailCommonBean> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int i) {
        return list == null ? null : list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return list == null ? 0 : i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null) {
//            view = LayoutInflater.from(context).inflate(R.layout.item_common_layout, null);
            view = LayoutInflater.from(context).inflate(R.layout.video_dis_comment_view, null);
            viewHolder = new ViewHolder(view);

            view.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        viewHolder.bind(i);

        return view;
    }

    class ViewHolder {

//        private ImageView iv_face;
//        private TextView tv_name;
//        private TextView tv_time;
//        private TextView tv_like;
//        private TextView tv_comment;
        private ImageView videoDisCommentImg;
        private TextView videoDisCommentName;
        private TextView videoDisCommentMsg;
        private TextView videoDisCommentTime;
        private LinearLayout videoCommentDZLl;
        private ImageView videoDisCommentDZTip;
        private TextView videoDisCommentDZNum;
        ViewHolder(View view) {

//            iv_face = item.findViewById(R.id.iv_face);
//            tv_name = item.findViewById(R.id.tv_name);
//            tv_time = item.findViewById(R.id.tv_time);
//            tv_like = item.findViewById(R.id.tv_like);
//            tv_comment = item.findViewById(R.id.tv_comment);

            videoDisCommentImg = view.findViewById(R.id.videoDisCommentImg);

            videoDisCommentName = view.findViewById(R.id.videoDisCommentName);
            videoDisCommentMsg = view.findViewById(R.id.videoDisCommentMsg);
            videoDisCommentTime = view.findViewById(R.id.videoDisCommentTime);
            videoCommentDZLl = view.findViewById(R.id.videoCommentDZLl);
            videoDisCommentDZTip = view.findViewById(R.id.videoDisCommentDZTip);
            videoDisCommentDZNum = view.findViewById(R.id.videoDisCommentDZNum);

        }

        @SuppressLint("SetTextI18n")
        void bind(int i) {
            final ImageDetailCommonBean bean = list.get(i);

            //显示评论人的图片


            if (TextUtils.isEmpty(bean.getHead_url())) {

//                String url = "http://a0.att.hudong.com/16/91/300001062059131423910670544_950.jpg";
                Glide.with(context)
                        .load(R.drawable.default_img)
                        .asBitmap()
                        .transform(new GlideCircleTransform(context))
                        .into(videoDisCommentImg);

            } else {
                Glide.with(context)
                        .load(bean.getHead_url())
                        .asBitmap()
                        .transform(new GlideCircleTransform(context))
                        .into(videoDisCommentImg);
            }


            //显示品论人的名字
            videoDisCommentName.setText(bean.getNick_name());

            //显示评论内容
            videoDisCommentMsg.setText(bean.getComment());

            //点赞个数
            videoDisCommentDZNum.setText(String.valueOf(bean.getLikes_num()));

            //显示时间
            videoDisCommentTime.setText(bean.getTime());

            videoCommentDZLl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    ToastUtil.showShortToast("点赞了");
                    if (bean.getHas_liked()==0) {

                        if (context instanceof ImageDetailActivity) {
                            ImageDetailActivity activity = (ImageDetailActivity) context;

                            if (activity.isLogin()) {
                                likeFun(bean.getComment_id()+"", bean);
                            }

                        }

                    } else {
                        ToastUtil.showShortToast("您已点赞");
                    }


                }
            });
            //是否点赞
            if (bean.getHas_liked()==1) {
                //点赞了
                videoDisCommentDZTip.setImageResource(R.mipmap.video_dis_comment);
            } else {
                //没点赞
                videoDisCommentDZTip.setImageResource(R.mipmap.video_dis_comment_1);
            }
        }
    }



    /**
     * 点赞接口
     *
     * @param id 评论的ID
     */
    private void likeFun(String id, final ImageDetailCommonBean bean) {

        PictureDataRequest.likePictureComment(id, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (context != null) {
                    ToastUtil.showShortToast("点赞失败");
                }
            }

            @Override
            public void onResponse(String response, int id) {

                try {
                    JSONObject jsonObject = new JSONObject(response);

                    if (!jsonObject.isNull("errno")) {

                        String code = jsonObject.getString("errno");
                        if ("0".equals(code)) {
                            if (context != null) {
                                ToastUtil.showShortToast("点赞成功");
                                bean.setHas_liked(1);

                                int cout = bean.getLikes_num();
                                bean.setLikes_num(cout + 1);

                                notifyDataSetChanged();
                            }

                        } else {
                            if (context != null) {
                                ToastUtil.showShortToast("点赞失败");
                            }
                        }

                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });

    }
}
